package com.whacos.common.shiro;

import com.whacos.common.utils.ObjectUtils;
import com.whacos.modules.system.entity.SysUser;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.eis.SessionDAO;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;

import java.security.Principal;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

/**
 * @author xiongdun
 * @description ShiroUtils
 * @since 2018/11/17 17:11
 */
public class ShiroUtils {

    @Autowired
    private static SessionDAO sessionDAO;

    public static Subject getSubject() {
        return SecurityUtils.getSubject();
    }

    public static SysUser getUser() {
        Object object = ShiroUtils.getSubject().getPrincipal();
        return (SysUser) object;
    }

    public static Long getUserId() {
        return ObjectUtils.isNotEmpty(ShiroUtils.getUser()) ? ShiroUtils.getUser().getId() : null;
    }

    public static void logout() {
        ShiroUtils.getSubject().logout();
    }

    public static List<Principal> getPrinciples() {
        List<Principal> principals = null;
        Collection<Session> sessions = sessionDAO.getActiveSessions();
        return principals;
    }
}
